package com.thinking.dubbo.redis.user;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

public class UserRedisTemplate<K, V> extends RedisTemplate<K, V> {

    public UserRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(om.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        setKeySerializer(RedisSerializer.string());
        setValueSerializer(jackson2JsonRedisSerializer);
        setHashKeySerializer(jackson2JsonRedisSerializer);
        setHashValueSerializer(jackson2JsonRedisSerializer);
        setConnectionFactory(lettuceConnectionFactory);
        afterPropertiesSet();
    }

}
